package com.mediacloud.app.newsmodule.activity

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.Component105type1ActivityAdapter
import com.mediacloud.app.newsmodule.drama.RadioRankFindList
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi
import com.mediacloud.app.newsmodule.utils.SBShareUtils
import com.mediacloud.app.newsmodule.utils.share.ShareWrap
import com.utils.RxUtils
import com.zimeiti.utils.ActUtil
import kotlinx.android.synthetic.main.activity_component105type1.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class Component105Type1Activity : BaseBackActivity(), RxUtils.OnClickInterf {
    override fun getFitSystemWindow(): Boolean = false

    override fun getStatusBarColor(): Int = Color.TRANSPARENT
    override fun getLayoutResID(): Int {
        return R.layout.activity_component105type1
    }

    private var adapter: Component105type1ActivityAdapter? = null
    private var type = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        type = intent.getIntExtra(ActUtil.DATA, 1)
        tv_title?.text = if (type == 1) {
            img_bg?.setBackgroundResource(R.color.red)
            "热门榜"
        } else if (type == 2) {
            img_bg?.setBackgroundResource(R.color.red)
            "热歌榜"
        } else if (type == 3) {
            "达人榜"
        } else {
            ""
        }
        adapter = Component105type1ActivityAdapter(type)
        recyclerView?.layoutManager = LinearLayoutManager(this)
        recyclerView?.adapter = adapter
        /*fresh?.setEnableLoadMore(false)
        fresh?.setEnableAutoLoadMore(false)
        fresh?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                getData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
            }

        })*/
        img_bg?.post {
            nestedScrollView?.setOnScrollChangeListener(object :
                View.OnScrollChangeListener {
                override fun onScrollChange(
                    v: View?,
                    scrollX: Int,
                    scrollY: Int,
                    oldScrollX: Int,
                    oldScrollY: Int
                ) {
                    if (scrollY > img_bg.height / 2) {
                        if (img_back?.colorFilter == null) {
                            img_back?.setColorFilter(Color.BLACK)
                            img_more?.setColorFilter(Color.BLACK)
                            view_mask?.setBackgroundColor(Color.WHITE)
                            tv_title?.visibility = View.VISIBLE
                        }
                    } else {
                        if (img_back?.colorFilter != null) {
                            img_back?.clearColorFilter()
                            img_more?.clearColorFilter()
                            view_mask?.setBackgroundColor(Color.TRANSPARENT)
                            tv_title?.visibility = View.GONE
                        }
                    }
                }
            })
        }

        RxUtils.rxClick(img_back, this)
        RxUtils.rxClick(net_error, this)
        RxUtils.rxClick(img_more, this)
    }

    override fun onResume() {
        super.onResume()
        getData()
    }

    override fun onViewClick(view: View?) {
        when (view?.id) {
            R.id.img_back -> {
                finish()
            }
            R.id.net_error -> {
                getData()
            }
            R.id.img_more -> {
                var shareWrap = ShareWrap(this, false)
                shareWrap?.setShowFunction(false)
                //val articleItem = ArticleItem()
                shareWrap?.init(null, null)
                shareWrap?.removeGridItem(SBShareUtils.SharePoster, SBShareUtils.CopyLink)
                shareWrap?.show(rootView)
            }
        }
    }

    fun getData() {
        SpiderCmsApi.getSpiderCmsApi(this).radioRankFindList(
            getString(R.string.tenantid),
            10,
            type
        )
            .enqueue(object : Callback<RadioRankFindList> {
                override fun onResponse(
                    call: Call<RadioRankFindList>,
                    response: Response<RadioRankFindList>
                ) {
                    // fresh?.finishRefresh()
                    // fresh?.finishLoadMore()
                    if (response.body() != null
                        && response.body()?.state == 200
                        && response.body()?.data != null
                        && response.body()?.data!!.size > 0
                    ) {
                        tv_time?.text = "更新时间：" + response.body()?.data!!.get(0).updateTime
                        net_error?.visibility = View.GONE
                        adapter?.setNewData(response.body()?.data!!.get(0).rankList)
                    } else {
                        net_error?.visibility = View.VISIBLE
                    }
                }

                override fun onFailure(call: Call<RadioRankFindList>, t: Throwable) {
                    //  fresh?.finishRefresh()
                    //  fresh?.finishLoadMore()
                    net_error?.visibility = View.VISIBLE
                }
            })
    }


}